﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BussinessLogicLayer.BussinessObject;
using DataAccessLayer;

namespace BussinessLogicLayer.ChucNangQLKhoa.CaiDat
{
    class CnQuanLyKhoa:ICNQuanLyKhoa
    {

        public NguoiQLKhoa_Khoa LayThongTinNguoiQuanLyKhoa(string maNgQnLyKhoa)
        {
            TinChiEntitiesDataContext tinChiEntities = Service.TinChiDataContext;
            NguoiQLKhoa_Khoa qlk;
            qlk =tinChiEntities.QLKHOAs.Where(qlykhoa => qlykhoa.MaNV == maNgQnLyKhoa).Select(
                    qlykhoa => new NguoiQLKhoa_Khoa
                                   {
                                       MaNguoiQLyKhoa = qlykhoa.MaNV,
                                       Hoten = qlykhoa.Hoten,
                                       MaKhoa = qlykhoa.MaKhoa,
                                       //Lay ten khoa thong qua ma khoa
                                       //Kiem tra lai cho nay
                                       TenKhoa = qlykhoa.KHOA.Tenkhoa,
                                       QueQuan = qlykhoa.Quequan,
                                       GioiTinh = qlykhoa.Gioitinh.Value,
                                       NgaySinh = qlykhoa.Ngaysinh.Value
                                   }).First();
            return qlk;
        }

        public List<DiemSvBo> LayDsDiemSv(NguoiQLKhoa_Khoa NguoiQlyKhoa, string malophp)
        {
            TinChiEntitiesDataContext tinChiEntities = Service.TinChiDataContext;
            List<DiemSvBo> dsDiem =
                tinChiEntities.DIEMSVs.Where(mlop => mlop.MaLopHP == malophp).Select(mlop => new DiemSvBo
                                                                                                 {
                                                                                                     MaDiem = mlop.MaDiem,
                                                                                                     MaSv = mlop.MaSV,
                                                                                                     MaLopHp = mlop.MaLopHP,
                                                                                                     DiemCc = mlop.Diemchuyencan.Value,
                                                                                                     DiemGiuaKy = mlop.Diemgiuaky.Value,
                                                                                                     DiemCuoiKy = mlop.Diemcuoiky.Value,
                                                                                                     DiemTongKet = mlop.Diemchuyencan.Value
                                                                                                 }).ToList();
            return dsDiem;
        }

        public List<string> LayDsMaLopHp(NguoiQLKhoa_Khoa NguoiQlyKhoa)
        {
            TinChiEntitiesDataContext tinChiEntities = Service.TinChiDataContext;
            List<string> ds = tinChiEntities.LOPHPs.Select(k => k.MaLopHP).ToList();
            return ds;
        }

        public List<SinhVienBo> LayDsSinhVien(NguoiQLKhoa_Khoa NguoiQlyKhoa)
        {
            TinChiEntitiesDataContext tinChiEntities = Service.TinChiDataContext;
            List<SinhVienBo> dssv = tinChiEntities.SINHVIENs.Select(sv => new SinhVienBo
                                                                              {
                                                                                  MaSv = sv.MaSV,
                                                                                  HoTen = sv.Hoten,
                                                                                  GioiTinh = sv.Gioitinh.Value,
                                                                                  QueQuan = sv.Quequan,
                                                                                  NgaySinh = sv.Ngaysinh.Value,
                                                                                  MaKhoa = sv.MaKhoa
                                                                              }).ToList();
            return dssv;
        }

        public List<GiaoVienBo> LayDsGiaoVien(NguoiQLKhoa_Khoa NguoiQlyKhoa)
        {
            TinChiEntitiesDataContext tinChiEntities = Service.TinChiDataContext;
            List<GiaoVienBo> dsGv = tinChiEntities.GIAOVIENs.Select(gv => new GiaoVienBo
                                                                              {
                                                                                  MaGv = gv.MaGV,
                                                                                  MaKhoa = gv.MaKhoa,
                                                                                  HoTen = gv.Hoten,
                                                                                  GioiTinh = gv.Gioitinh.Value,
                                                                                  NgaySinh = gv.Ngaysinh.Value,
                                                                                  QueQuan = gv.Quequan,
                                                                                  ChuyenNganh = gv.Chuyennganh
                                                                              }).ToList();
            return dsGv;
        }

        public void QlySinhVienThem(NguoiQLKhoa_Khoa NguoiQlyKhoa, SinhVienBo Sv)
        {
            TinChiEntitiesDataContext tinChiEntities = Service.TinChiDataContext;
            SINHVIEN sVien=new SINHVIEN();
            sVien.MaSV = Sv.MaSv;
            sVien.Hoten = Sv.HoTen;
            sVien.Gioitinh = Sv.GioiTinh;
            sVien.Quequan = Sv.QueQuan;
            sVien.Ngaysinh = Sv.NgaySinh;
            sVien.MaKhoa = Sv.MaKhoa;
            tinChiEntities.SINHVIENs.InsertOnSubmit(sVien);
            tinChiEntities.SubmitChanges();
        }

        public void QlySinhVienCapNhat(NguoiQLKhoa_Khoa NguoiQlyKhoa, SinhVienBo Sv)
        {
            TinChiEntitiesDataContext tinChiEntities = Service.TinChiDataContext;
            try
            {
                SINHVIEN sVien = new SINHVIEN();
                sVien.MaSV = Sv.MaSv;
                sVien.Hoten = Sv.HoTen;
                sVien.Gioitinh = Sv.GioiTinh;
                sVien.Quequan = Sv.QueQuan;
                sVien.Ngaysinh = Sv.NgaySinh;
                sVien.MaKhoa = Sv.MaKhoa;
                tinChiEntities.SubmitChanges();
            }
            catch (Exception)
            {

                SINHVIEN sVien = new SINHVIEN();
                sVien.MaSV = Sv.MaSv;
                sVien.Hoten = Sv.HoTen;
                sVien.Gioitinh = Sv.GioiTinh;
                sVien.Quequan = Sv.QueQuan;
                sVien.Ngaysinh = Sv.NgaySinh;
                sVien.MaKhoa = Sv.MaKhoa;
                tinChiEntities.SINHVIENs.InsertOnSubmit(sVien);
                tinChiEntities.SubmitChanges();
            }
        }

        public void QlySinhVienXoa(NguoiQLKhoa_Khoa NguoiQlyKhoa, string Masv)
        {
            TinChiEntitiesDataContext tinChiEntities = Service.TinChiDataContext;
            SINHVIEN sv = tinChiEntities.SINHVIENs.Single(svien => svien.MaSV == Masv);
            tinChiEntities.SINHVIENs.DeleteOnSubmit(sv);
            tinChiEntities.SubmitChanges();
        }

        public void QlyGiaoVienThem(NguoiQLKhoa_Khoa NguoQlKhoa, GiaoVienBo Gv)
        {
            TinChiEntitiesDataContext tinChiEntities = Service.TinChiDataContext;
            GIAOVIEN gvien=new GIAOVIEN();
            gvien.MaGV = Gv.MaGv;
            gvien.Hoten = Gv.HoTen;
            gvien.Ngaysinh = Gv.NgaySinh;
            gvien.Chuyennganh = Gv.ChuyenNganh;
            gvien.Gioitinh = Gv.GioiTinh;
            gvien.Quequan = Gv.QueQuan;
            gvien.MaKhoa = Gv.MaKhoa;
            tinChiEntities.GIAOVIENs.InsertOnSubmit(gvien);
            tinChiEntities.SubmitChanges();
        }

        public void QlyGiaoVienCapNhat(NguoiQLKhoa_Khoa NguoQlKhoa, GiaoVienBo Gv)
        {
            TinChiEntitiesDataContext tinChiEntities = Service.TinChiDataContext;
            try
            {
                GIAOVIEN gvien = new GIAOVIEN();
                gvien.MaGV = Gv.MaGv;
                gvien.Hoten = Gv.HoTen;
                gvien.Ngaysinh = Gv.NgaySinh;
                gvien.Chuyennganh = Gv.ChuyenNganh;
                gvien.Gioitinh = Gv.GioiTinh;
                gvien.Quequan = Gv.QueQuan;
                gvien.MaKhoa = Gv.MaKhoa;
                tinChiEntities.SubmitChanges();
            }
            catch (Exception)
            {
                GIAOVIEN gvien = new GIAOVIEN();
                gvien.MaGV = Gv.MaGv;
                gvien.Hoten = Gv.HoTen;
                gvien.Ngaysinh = Gv.NgaySinh;
                gvien.Chuyennganh = Gv.ChuyenNganh;
                gvien.Gioitinh = Gv.GioiTinh;
                gvien.Quequan = Gv.QueQuan;
                gvien.MaKhoa = Gv.MaKhoa;
                tinChiEntities.GIAOVIENs.InsertOnSubmit(gvien);
                tinChiEntities.SubmitChanges();
            }
        }

        public void QlyGiaoVienXoa(NguoiQLKhoa_Khoa NguoiQlKhoa, string MaGv)
        {
            TinChiEntitiesDataContext tinChiEntities = Service.TinChiDataContext;
            GIAOVIEN gvien = tinChiEntities.GIAOVIENs.Single(gv => gv.MaGV == MaGv);
            tinChiEntities.GIAOVIENs.DeleteOnSubmit(gvien);
            tinChiEntities.SubmitChanges();
        }

        public void QlyDiemThem(NguoiQLKhoa_Khoa NguoiQlyKhoa, string malophp, DiemSvBo DiemSv)
        {
            TinChiEntitiesDataContext tinChiEntities = Service.TinChiDataContext;
            DIEMSV dsv=new DIEMSV();
            dsv.MaDiem = DiemSv.MaDiem;
            dsv.MaSV = DiemSv.MaSv;
            dsv.MaLopHP = DiemSv.MaLopHp;
            dsv.Diemchuyencan = DiemSv.DiemCc;
            dsv.Diemgiuaky = DiemSv.DiemGiuaKy;
            dsv.Diemcuoiky = DiemSv.DiemCuoiKy;
            dsv.Diemtongket = DiemSv.DiemTongKet;
            tinChiEntities.DIEMSVs.InsertOnSubmit(dsv);
            tinChiEntities.SubmitChanges();
        }

        public void QlyDiemCapNhat(NguoiQLKhoa_Khoa NguoiQlyKhoa, string malophp, DiemSvBo DiemSv)
        {
            TinChiEntitiesDataContext tinChiEntities = Service.TinChiDataContext;
            try
            {
                DIEMSV dsv = new DIEMSV();
                dsv.MaDiem = DiemSv.MaDiem;
                dsv.MaSV = DiemSv.MaSv;
                dsv.MaLopHP = DiemSv.MaLopHp;
                dsv.Diemchuyencan = DiemSv.DiemCc;
                dsv.Diemgiuaky = DiemSv.DiemGiuaKy;
                dsv.Diemcuoiky = DiemSv.DiemCuoiKy;
                dsv.Diemtongket = DiemSv.DiemTongKet;
                tinChiEntities.SubmitChanges();
            }
            catch (Exception)
            {
                DIEMSV dsv = new DIEMSV();
                dsv.MaDiem = DiemSv.MaDiem;
                dsv.MaSV = DiemSv.MaSv;
                dsv.MaLopHP = DiemSv.MaLopHp;
                dsv.Diemchuyencan = DiemSv.DiemCc;
                dsv.Diemgiuaky = DiemSv.DiemGiuaKy;
                dsv.Diemcuoiky = DiemSv.DiemCuoiKy;
                dsv.Diemtongket = DiemSv.DiemTongKet;
                tinChiEntities.DIEMSVs.InsertOnSubmit(dsv);
                tinChiEntities.SubmitChanges();
            }
        }

        public void QlyDiemXoa(NguoiQLKhoa_Khoa NguoiQlyKhoa, string malophp, int maDiem)
        {
            TinChiEntitiesDataContext tinChiEntities = Service.TinChiDataContext;
            DIEMSV dsv = tinChiEntities.DIEMSVs.Single(diemsv => diemsv.MaDiem == maDiem);
            tinChiEntities.DIEMSVs.DeleteOnSubmit(dsv);
            tinChiEntities.SubmitChanges();
        }
    }
}
